﻿<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <meta http-equiv="X-UA-Compatible" content="IE=EDGE;IE=11;IE=8,chrome=1" />
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>MES自定义对象子项维护</title>
  <link href="/InSiteWebApplication/Content/Easyui/themes/metro/easyui.css" rel="stylesheet" />
  <link href="/InSiteWebApplication/Content/Easyui/custom-easyui.css" rel="stylesheet" />
  <link href="/InSiteWebApplication/Content/Easyui/themes/icon.css" rel="stylesheet" />
  
</head>

<body>
  <!-- 查询区域start -->
  <div class="panelSection" id="queryZone">
    <div class="easyui-panel " title="查询区域" style="padding:3px 3px 5px;width: 100%"
      data-options="closable:false,collapsible:true,minimizable:false,maximizable:false">
      <div>
        <div style="display:inline-block" >
          <div class="divLabel" style="margin-bottom: 5px;">父对象名称</div>
          <input id='ddlCumtomObject' data-options='disabled:true,panelHeight:"auto",valueField:"customobjectid",textField: "customobjectname"' class='easyui-combobox' style="width: 200px; height: 22px"/>
        </div>
        <div style="display:inline-block;margin-left:10px">
          <div class="divLabel" style="margin-bottom: 5px;">子项值:</div>
          <input id="txtCustomObjectItemsName" class="easyui-textbox" style="width: 150px;height:22px  "/>
        </div>
        <div style="display:inline-block;margin-left:10px">
          <div class="divLabel" style="margin-bottom: 5px;">子项描述:</div>
          <input id="txtCustomObjectItemsDesc" class="easyui-textbox" style="width: 150px;height:22px  "/>
        </div>
        <div style="display:inline-block;margin-left:10px">
          <div class="divLabel" style="margin-bottom: 5px;">创建人:</div>
          <input id="txtCreateEmp" class="easyui-textbox" style="width:100px;height:22px " />
        </div>
        <div style="display:inline-block;margin-left:10px">
          <div class="divLabel" style="margin-bottom: 5px;">创建时间:</div>
          <input id="createdateStart" type="text" class="easyui-datebox" style="width:120px;height:22px " />
        </div>
        <div style="display:inline-block;margin-left:10px">
          <div class="divLabel" style="margin-bottom: 5px;">至:</div>
          <input id="createdateEnd" type="text" class="easyui-datebox" style="width:120px;height:22px " />
        </div>
        <div style="display:inline-block;margin-left:10px">
          <div class="divLabel" style="margin-bottom: 5px;">启用状态</div>
          <select id='ddlIsuse' data-options='editable:false,panelHeight:"auto"' class='easyui-combobox select' style="width:80px;height:22px ">
            <option value="全部">全部</option>
            <option value="0">启用</option>
            <option value="1">停用</option>
          </select>
        </div>
        <button id="btnSearch" class="easyui-linkbutton" data-options="">查 询</button>
        <button id="btnReset" class="easyui-linkbutton" data-options="">重 置</button>
      </div>
    </div>
  </div>
  <!-- 查询区域end -->

  <!-- 查询结果区域start -->
  <div class="panelSection" id="dataGridZone">
    <div  class="easyui-panel " title="查询结果" style="padding: 3px;width: 100%"
      data-options="closable:false,collapsible:true,minimizable:false,maximizable:false">
        <table id="tbCustomObject" class="easyui-datagrid" style="height: 300px;" data-options="rownumbers:true,singleSelect:true">
        </table>
        <div id="pp" style="float: right" class="easyui-pagination" data-options="   
                layout:['first','prev','next','last','manual','info'],              
                total: 0,
                showPageList: false,
                showPageInfo: false,
                showRefresh: false,
                afterPageText:'共&nbsp{pages}&nbsp页',
                    buttons:[{
                        iconCls:'',
                        text:'Go',
                        group:'paging-tail-btns',
                        id:'pageGo'
                }]
            ">
        </div>
    </div>
  </div>
  <div id="gd-tb">
    <a href="#" class="easyui-linkbutton not-custom" data-options="iconCls:'icon-add',plain:false" onclick="page.openItemForm('',1)" style="">添加</a> 
  </div>

    <div id="dg" style="display:none">
        <div style="margin-left: 10px;">
            <div style="margin-top:5px">
                <div class="divLabel" style="margin-bottom: 5px; ">
                    子项值：</div>
                <input id="dg-txtItemName" class="easyui-textbox"  
                    data-options="required:true,missingMessage:'该字段是必填的'" style="width: 150px;height:22px " />
            </div>
            <div style="margin-top:10px">
                <div class="divLabel" style="margin-bottom: 5px; ">
                    子项描述：</div>
                <input id="dg-txtItemDesc" class="easyui-textbox"  
                    data-options="required:true,missingMessage:'该字段是必填的'" style="width: 150px;height:22px  " />
            </div>
            
            <div style="margin-top:10px">
                <div class="divLabel" style="margin-bottom: 5px;  ">
                顺序号：</div>
                <input id="dg-txtItemSeq" class="easyui-numberbox"  data-options=""
                    style="width: 150px; height: 22px" />         
            </div>
            <div style="margin-top:10px">
                <div class="divLabel" style="margin-bottom: 5px;  ">
                所属分厂：</div>
                <input id="dg-ddlFactory" class="easyui-combobox"  data-options="valueField:'FACTORYID',textField:'FACTORYNAME'"
                    style="width: 200px; height: 22px" />         
            </div>
            <div style="margin-top:10px">
                <div class="divLabel" style="margin-bottom: 5px;  ">
                启用：</div>     
                <input id="dg-sbIsUse" class="easyui-switchbutton" data-options="checked:true,onText:'开',offText:'关'" checked style="width:70px;height:20px">    
            </div>
            <div style="margin-top:10px">
                <div class="divLabel" style="margin-bottom: 5px; ">
                    备注：</div>
                <input id="dg-txtItemNotes" class="easyui-textbox" 
                    data-options="multiline:true,width:270,height:60" style="width:270px;height:60px" />
            </div>
            <div style="text-align:center;margin-top:20px" >            
                <button id="dg-btnSaveItem" class="easyui-linkbutton" data-options="" style="width:100px">保 存</button>  
            </div>             
        </div>
    </div>
</body>

</html>
<script src="/InSiteWebApplication/Scripts/CustomPageJs/ReferenceJsLib.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Content/Easyui/jquery.min.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Content/Easyui/jquery.easyui.min.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Content/Easyui/easyui-lang-zh_CN.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Scripts/CustomPageJs/CustomCommon.js" type="text/javascript"></script>
<script type="text/javascript">
    /*
    创建人：wangjh
    date:2023/4/20 
    */
    var page = {
        parentID: '', //父窗体div
        returnData: null, //返回的消息
        param: null, //传递的消息
        savingRow:{},
        pagetotal: 0,
        pageSize: 30,
        pageNumber: 1,
        commonAjaxUrl: getRootPath() + "/Ashx/CommonHandler.ashx", 
        mainUrl: getRootPath() + "/Ashx/CustomObjectItemsMaint.ashx", //本页面后台路径
        //初始化页面
        init: function () {
            page.bind();
            page.initData(); //初始化页面数据
        },
        // 页面事件绑定
        bind: function () {
            // 批次信息
            $("#tbCustomObject").datagrid({
                toolbar:'#gd-tb',
                columns: [[
        { field: 'ck', title: '选择', checkbox: true },
        { field: 'customobjectid', title: '父id', hidden: true },
        { field: 'customobjectitemsid', title: '唯一id', hidden: true },
        { field: 'customobjectname', title: '父对象名称', width: 100 },
        { field: 'customobjectitemsname', title: '子项值', width: 100 },
        { field: 'customobjectitemsdesc', title: '子项描述', width: 100 },
        { field: 'customobjectitemsnotes', title: '备注', width: 200 },
        {
            field: 'isuse', title: '启用状态', width: 60,
            formatter: function (value, row, index) {
                if (value === 0) {
                    return '启用';
                } else {
                    return '停用';
                }
            },
            styler: function (value) {
                if (value === 1) {
                    return 'color:red;';
                } else {
                    return 'color:green;';
                }
            }
        },
        { field: 'creater', title: '创建人', width: 70 },
        {
            field: 'createdate', title: '创建时间', width: 150,
            formatter: function (value, row, index) {
                if (value) {
                    return value.replace('T', ' ');
                } else {
                    return value;
                }
            }
        },
        { field: 'updater', title: '更新人', width: 70 },
        {
            field: 'lastchangedate', title: '更新时间', width: 150,
            formatter: function (value, row, index) {
                if (value) {
                    return value.replace('T', ' ');
                } else {
                    return value;
                }
            }
        }
         , { field: 'operate', title: '操作', width: 170, formatter: function (value, row, index) {
            var para = {
                customobjectid: row.customobjectid,
                customobjectitemsid:row.customobjectitemsid,
                customobjectitemsname:row.customobjectitemsname,
                customobjectitemsdesc: row.customobjectitemsdesc,
                customobjectitemsnotes:row.customobjectitemsnotes,
                isuse:row.isuse,
                factoryid: row.factoryid,
                sequence:row.sequence

            }

            var btnHtml =''; //'<button  class="easyui-linkbutton not-custom addRow"  onclick=page.openItemForm(\''+JSON.stringify(para)+'\',1) style="cursor:pointer;width: 60px; height: 22px">添加</button>';
             btnHtml +='<a   class="easyui-linkbutton c3 not-custom editRow"  onclick=page.openItemForm(\''+JSON.stringify(para)+'\',2) style="width: 60px; height: 22px">修改</a>';
            btnHtml += '<a  class="easyui-linkbutton not-custom delRow"  onclick=page.delItem(\''+JSON.stringify(para)+'\') style="width: 60px; height: 22px">删除</a>';
            
            return btnHtml
            }
        }
      ]],
                onLoadSuccess: function (data) {
                    $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                        total: page.pagetotal,
                        pageSize: page.pageSize,
                        pageNumber: page.pageNumber
                    });
                    customizedEasyuiPagination('pp'); //定制化分页
                    //按钮给样式
                    $("#dataGridZone .addRow").linkbutton({plain: true, iconCls: 'icon-add'});
                    $("#dataGridZone .editRow").linkbutton({plain: true, iconCls: 'icon-edit'});
                    $("#dataGridZone .delRow").linkbutton({plain: true, iconCls: 'icon-cancel'});
                },
                //行点击事件
                onClickRow: function (rowIndex, rowData) { }
            });

            //批次表格分页
            $('#pp').pagination({
                onSelectPage: function (pageNumber, pageSize) {
                    customizedEasyuiPagination('pp'); //定制化分页
                    page.pageSize = pageSize;
                    page.pageNumber = pageNumber;
                    page.search();
                }
            });

            //绑定分页go按钮
            $('#pageGo').on("click", function () {
                var pageNumber = $(this).parents('tr').find('.pagination-num').val();
                page.pageNumber = pageNumber;
                page.search();
            });

            // 查询
            $("#btnSearch").on("click", function () {
                page.pageNumber = 1;
                page.search();
            });

            // 保存
            $("#dg-btnSaveItem").on("click", function () {
                page.saveItem();
            });

            // 重置按钮
            $('#btnReset').on("click", function () {
                page.reset(true,true,true);
            });

        },
        getItemMaxSeq:function(parentId){
            var re=null;
             $.ajax({
                type: "GET",
                url: page.mainUrl+"?action=getItemsMaxSeq&customobjectid="+parentId,  
                cashe: false,
                async:false,
                success: function (result) { 
                    re= result;
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    /*错误信息处理*/
                    alert(jqXHR.statusText);
                }
            }); 
            return re;
        },
        openItemForm:function(para,type){//type:1添加，2修改
            page.reset(false,false,true);
            if(!isNullOrWhiteSpaceStr(para))
                page.savingRow=JSON.parse(para);
            page.savingRow.type=type;
            if(isNullOrWhiteSpaceStr($("#ddlCumtomObject").combobox('getValue'))){
                displayMessage("请选择对象",false);
                return;
            }
            //console.log(page.savingRow); 
            if(type==2){
                //数据赋值
                 $('#dg-txtItemName').textbox('setValue',page.savingRow.customobjectitemsname);
                 $('#dg-txtItemDesc').textbox('setValue',page.savingRow.customobjectitemsdesc);
                 $('#dg-txtItemNotes').textbox('setValue',page.savingRow.customobjectitemsnotes);
                 $('#dg-txtItemSeq').numberbox('setValue',page.savingRow.sequence);
                 $('#dg-ddlFactory').combobox('setValue',page.savingRow.factoryid); 
                 if(page.savingRow.isuse=="1"){                 
                    $("#dg-sbIsUse").switchbutton('uncheck');
                 }else{ 
                    $("#dg-sbIsUse").switchbutton('check');                    
                 }
                 $('#dg-txtItemName').textbox('disable');
            } 
            if(type==1){
                page.savingRow.customobjectitemsid=null;
                $('#dg-txtItemName').textbox('enable');
            }
            if(type==1||page.savingRow.sequence==null){
                var maxSeq=page.getItemMaxSeq($("#ddlCumtomObject").combobox('getValue'));
                if(maxSeq!=null)
                    $('#dg-txtItemSeq').numberbox('setValue',parseInt(maxSeq)); 
             }
            
            $('#dg').dialog({
                title: '数据编辑',
                fit: false,
                width:380,
                height:420,
                top: 10,
                closed: false,
                cache: false,
                modal: true,   
                onClose: function () { 

                }
            });

        },
        saveItem:function(){ 
            var para={};
            para.customobjectitemsname=$("#dg-txtItemName").textbox('getValue');
            para.customobjectitemsdesc=$("#dg-txtItemDesc").textbox('getValue');
            para.customobjectitemsnotes=$("#dg-txtItemNotes").textbox('getValue');
            para.sequence=$("#dg-txtItemSeq").numberbox('getValue');
            para.isuse=0;
            if(!$("#dg-sbIsUse").switchbutton('options').checked){
                para.isuse=1;
            }
            para.factoryid=$("#dg-ddlFactory").combobox('getValue');
            para.customobjectid=$("#ddlCumtomObject").combobox('getValue');
            para.customobjectitemsid=page.savingRow.customobjectitemsid;
            para.type=page.savingRow.type;//1添加，2修改
            if(isNullOrWhiteSpaceStr(para.customobjectitemsname)||isNullOrWhiteSpaceStr(para.customobjectitemsdesc)){
                displayMessage("请录入必填项！",false);
                return;
            } 
            para.action="saveItem";

            $.ajax({
                type: "Post",
                url: page.mainUrl,
                data: para,
                cashe: false,
                async: true,
                beforeSend: function () {
                    $.messager.progress({ text: '', msg: '处理中...' });
                },
                success: function (data) {
                    $.messager.progress('close');
                    var result = JSON.parse(data);
                    if (result.Result) {
                        displayMessage("保存成功", true);
                        //关闭窗口，刷新列表
                        $('#dg').dialog('close');
                        page.search();
                    } else {
                        displayMessage(result.Message, false);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $.messager.progress('close');
                    alert(XMLHttpRequest.responseText);
                }
            });

        },
        //删除数据
        delItem:function(para){
            $.messager.confirm({
                title: '警告',
			    msg: '确定要删除数据？',
			    ok: "删除",
			    cancel: "取消",			  
			    fn : function(r){
                    if(!r){
				        return;                                
                    }
                    var p=JSON.parse(para); 
                    $.ajax({
                        type: "Post",
                        url: page.mainUrl,
                        data: {action:"deleteItem",customobjectitemsid:p.customobjectitemsid},
                        cashe: false,
                        async: true,
                        beforeSend: function () {
                            $.messager.progress({ text: '', msg: '处理中...' });
                        },
                        success: function (data) {
                            $.messager.progress('close');
                            var result = JSON.parse(data);
                            if (result.Result) {
                                displayMessage("删除成功", true); 
                                page.search();
                            } else {
                                displayMessage(result.Message, false);
                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            $.messager.progress('close');
                            alert(XMLHttpRequest.responseText);
                        }
                    });
			    }
            });
            
        },
        //初始化页面
        initData: function () {
            page.getCustomObject();
            $("#dg-ddlFactory").combobox('reload',page.commonAjaxUrl+"?action=getFactory");
        },
        // 查询自定义对象
        getCustomObject: function () {
            $.ajax({
                type: "post",
                url: page.mainUrl + "?action=getCustomObject",
                data: { action: "getCustomObject" },
                cashe: false,
                dataType: 'json',
                success: function (res) {
                    if (res.Result) {
                        res.DBTable.unshift({ customobjectid: '', customobjectname: '' });
                        //$("#ddlCumtomObject").combobox('loadData',res.DBTable);

                        
                        $("#ddlCumtomObject").combobox({
                            valueField: 'customobjectid',
                            textField: 'customobjectname',//value: page.param.customobjectid,
                            panelHeight: 'auto',
                            data: res.DBTable,
                            onChange:function(newValue, oldValue){
                                page.reset(false,true,true);
                            },
                            onLoadSuccess:function(){
                                var sel=res.DBTable.find(function(row){
                                    return row.customobjectname==page.param.customobjectname
                                });   
                                if(sel!=null&& !isNullOrWhiteSpaceStr(sel.customobjectid)){
                                    $("#ddlCumtomObject").combobox('select',sel.customobjectid);
                                    //console.log('禁用');
                                    $("#ddlCumtomObject").combobox('disable');  
                                    //初始化查询
                                    page.search(1);                                     
                                }else{//未指定对象，可自由选择
                                    //console.log('启用');
                                    $("#ddlCumtomObject").combobox('enable');
                                } 
                            }
                        });
                        
                    }
                }
            });
        },
        // 查询
        search: function (pageNumber) {
            $('#tbCustomObject').datagrid('loadData', []);
            var createdateStart = "";
            if (!isNaN(dayjs($("#createdateStart").datebox('getValue')).$y)) {
                createdateStart = $("#createdateStart").datebox('getValue');
            }
            var createdateEnd = "";
            if (!isNaN(dayjs($("#createdateEnd").datebox('getValue')).$y)) {
                createdateEnd = $("#createdateEnd").datebox('getValue');
            }
            var formData = {
                action: "getCustomItems",
                pageSize: page.pageSize,
                pageNumber: pageNumber || page.pageNumber,
                customobjectid: $('#ddlCumtomObject').combobox('getValue'), //父项id
                customobjectitemsname: $("#txtCustomObjectItemsName").textbox('getValue'), //子项名称
                customobjectitemsdesc:$('#txtCustomObjectItemsDesc').textbox('getValue'),//子项描述
                creater: $("#txtCreateEmp").textbox('getValue'),
                createdateStart: createdateStart,
                createdateEnd: createdateEnd,
                isuse: $('#ddlIsuse').combobox('getValue')
            };

            $.ajax({
                type: "Post",
                url: page.mainUrl + "?actions=getData",
                data: formData,
                cashe: false,
                async: true,
                beforeSend: function () {
                    $('#tbCustomObject').datagrid('loading');
                    $(this).attr("disabled", "disabled"); //禁用按钮
                },
                success: function (data) {
                    $('#tbCustomObject').datagrid('loaded');
                    $(this).removeAttr("disabled"); //恢复按钮
                    var result = JSON.parse(data);
                    if (result.Result) {
                        //给分页对象赋值
                        page.pagetotal = result.RowCount;
                        $('#tbCustomObject').datagrid('loadData', { total: result.RowCount, rows: result.DBTable });
                    } else {
                        displayMessage(result.Message, false);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $('#tbCustomObject').datagrid('loaded');
                    $(this).removeAttr("disabled"); //恢复按钮
                    alert(XMLHttpRequest.responseText);
                }
            });
        },       
        // 清除方法，重要！！！
        reset: function (iscondition, isresult,isDetail) {
            clearMessage();
            if(iscondition){
                $('#txtCustomObjectItemsName').textbox('clear');
                $('#txtCreateEmp').textbox('clear');
                $('#createdateStart').textbox('clear');
                $('#createdateEnd').textbox('clear');
                $('#txtCustomObjectItemsDesc').textbox('clear');
            }
            if (isresult) {
                clearEasyuiDataGrid("tbCustomObject", 1);
            }
            if(isDetail){      
                page.savingRow={};          
                $('#dg-txtItemName').textbox('clear');
                $('#dg-txtItemDesc').textbox('clear');
                $('#dg-txtItemNotes').textbox('clear');
                $('#dg-txtItemSeq').numberbox('clear');
                $('#dg-ddlFactory').combobox('select',"");
                $("#dg-sbIsUse").switchbutton('check');   
            }
        },
        
    };
    $(function () {
        page.parentID = getParam("parentDiv");
        if(!isNullOrWhiteSpaceStr(page.parentID))//弹出页面
        {
            page.param = parent.$("#" + page.parentID).dialog('options').popupData;       
        }else{  //配置为一级页面    
            page.param={};  
            page.param.customobjectname=decodeURIComponent(getParam("objName"));//传参的对象

        }

        page.init();
    });
</script>